package com.yipei.business.service.impl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yipei.business.domain.MlAccountMessage;
import com.yipei.business.mapper.MlAccountMessageMapper;
import com.yipei.business.service.IMlAssetFlowService;
import com.yipei.business.service.IMlTradeAccountService;
import com.yipei.common.core.domain.entity.MlTradeAccount;
import com.yipei.common.utils.DateUtils;
import com.yipei.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yipei.business.mapper.MlAccountAssetApplyMapper;
import com.yipei.business.domain.MlAccountAssetApply;
import com.yipei.business.service.IMlAccountAssetApplyService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 出入资金Service业务层处理
 *
 * @author yipei
 * @date 2024-12-21
 */
@Service
public class MlAccountAssetApplyServiceImpl extends ServiceImpl<MlAccountAssetApplyMapper, MlAccountAssetApply> implements IMlAccountAssetApplyService
{
    @Autowired
    private IMlAssetFlowService iMlAssetFlowService;
    @Autowired
    private MlAccountAssetApplyMapper mlAccountAssetApplyMapper;

    @Autowired
    private IMlTradeAccountService iMlTradeAccountService;
    /**
     * 查询出入资金
     *
     * @param id 出入资金主键
     * @return 出入资金
     */
    @Override
    public MlAccountAssetApply selectMlAccountAssetApplyById(Long id)
    {
        return mlAccountAssetApplyMapper.selectMlAccountAssetApplyById(id);
    }

    /**
     * 查询出入资金列表
     *
     * @param mlAccountAssetApply 出入资金
     * @return 出入资金
     */
    @Override
    public List<MlAccountAssetApply> selectMlAccountAssetApplyList(MlAccountAssetApply mlAccountAssetApply)
    {
        return mlAccountAssetApplyMapper.selectMlAccountAssetApplyList(mlAccountAssetApply);
    }

    /**
     * 新增出入资金
     *
     * @param mlAccountAssetApply 出入资金
     * @return 结果
     */
    @Override
    public int insertMlAccountAssetApply(MlAccountAssetApply mlAccountAssetApply)
    {
        MlTradeAccount mlTradeAccount = SecurityUtils.getLoginMember().getMlTradeAccount();
        mlAccountAssetApply.setCreateTime(DateUtils.getNowDate());
        mlAccountAssetApply.setCrZt(new BigDecimal(0));
        mlAccountAssetApply.setTdAcc(mlTradeAccount.getTdAcc());
        mlAccountAssetApply.setTdAccId(Long.valueOf(mlTradeAccount.getId()));
        return mlAccountAssetApplyMapper.insertMlAccountAssetApply(mlAccountAssetApply);
    }

    /**
     * 修改出入资金
     *
     * @param mlAccountAssetApply 出入资金
     * @return 结果
     */
    @Override
    public int updateMlAccountAssetApply(MlAccountAssetApply mlAccountAssetApply)
    {
        if (mlAccountAssetApply.getCrZt().compareTo(BigDecimal.ONE)==0){
            LambdaQueryWrapper<MlTradeAccount> mlTradeAccountLambdaQueryWrapper=new LambdaQueryWrapper<>();
            mlTradeAccountLambdaQueryWrapper.eq(MlTradeAccount::getTdAcc,mlAccountAssetApply.getTdAcc());
            MlTradeAccount one = iMlTradeAccountService.getOne(mlTradeAccountLambdaQueryWrapper);
            if (mlAccountAssetApply.getCrLx().compareTo(BigDecimal.ZERO)==0){
                one.setKqZj(one.getKqZj().add(mlAccountAssetApply.getCash()));
                iMlAssetFlowService.creatObject(one.getTdAcc(), 0, mlAccountAssetApply.getCash());
                one.setCash((one.getCash()==null?BigDecimal.ZERO:one.getCash()).add(mlAccountAssetApply.getCash()));
            }else {
                one.setKqZj(one.getKqZj().subtract(mlAccountAssetApply.getCash()));
                iMlAssetFlowService.creatObject(one.getTdAcc(), 1, mlAccountAssetApply.getCash());
                one.setCash((one.getCash()==null?BigDecimal.ZERO:one.getCash()).subtract(mlAccountAssetApply.getCash()));
            }
            iMlTradeAccountService.updateById(one);
        }
        mlAccountAssetApply.setUpdateTime(DateUtils.getNowDate());
        return mlAccountAssetApplyMapper.updateMlAccountAssetApply(mlAccountAssetApply);
    }

    /**
     * 批量删除出入资金
     *
     * @param ids 需要删除的出入资金主键
     * @return 结果
     */
    @Override
    public int deleteMlAccountAssetApplyByIds(Long[] ids)
    {
        return mlAccountAssetApplyMapper.deleteMlAccountAssetApplyByIds(ids);
    }

    /**
     * 删除出入资金信息
     *
     * @param id 出入资金主键
     * @return 结果
     */
    @Override
    public int deleteMlAccountAssetApplyById(Long id)
    {
        return mlAccountAssetApplyMapper.deleteMlAccountAssetApplyById(id);
    }

    @Override
    @Transactional
    public int add(MlAccountAssetApply mlAccountAssetApply) throws Exception {
        if (mlAccountAssetApply.getCrZt().compareTo(BigDecimal.ONE)==0){
            LambdaQueryWrapper<MlTradeAccount> mlTradeAccountLambdaQueryWrapper=new LambdaQueryWrapper<>();
            mlTradeAccountLambdaQueryWrapper.eq(MlTradeAccount::getTdAcc,mlAccountAssetApply.getTdAcc());
            MlTradeAccount one = iMlTradeAccountService.getOne(mlTradeAccountLambdaQueryWrapper);
            mlAccountAssetApply.setTdAccId(Long.valueOf(one.getId()));
            if (ObjectUtils.isEmpty(one)){
                throw new Exception("交易账号不对，请重新输入");
            }
            if (mlAccountAssetApply.getCrLx().compareTo(BigDecimal.ZERO)==0){
                one.setKqZj(one.getKqZj().add(mlAccountAssetApply.getCash()));
                iMlAssetFlowService.creatObject(one.getTdAcc(), 0, mlAccountAssetApply.getCash());
                one.setCash((one.getCash()==null?BigDecimal.ZERO:one.getCash()).add(mlAccountAssetApply.getCash()));
            }else {
                one.setKqZj(one.getKqZj().subtract(mlAccountAssetApply.getCash()));
                iMlAssetFlowService.creatObject(one.getTdAcc(), 1, mlAccountAssetApply.getCash());
                one.setCash((one.getCash()==null?BigDecimal.ZERO:one.getCash()).add(mlAccountAssetApply.getCash()));
            }
            iMlTradeAccountService.updateById(one);
        }
        mlAccountAssetApply.setCreateTime(new Date());
        mlAccountAssetApplyMapper.insertMlAccountAssetApply(mlAccountAssetApply);
        return 1;
    }

    @Override
    public int cancelMlAccountAssetApplyById(Long id) {
        MlAccountAssetApply mlAccountAssetApply = this.lambdaQuery().eq(MlAccountAssetApply::getId, id).one();
        MlTradeAccount mlTradeAccount = iMlTradeAccountService.lambdaQuery().eq(MlTradeAccount::getId,mlAccountAssetApply.getTdAccId() ).one();
        mlAccountAssetApply.setCrZt(new BigDecimal(3));
        mlTradeAccount.setCash(mlTradeAccount.getCash().subtract(mlAccountAssetApply.getCash()));
        mlTradeAccount.setKqZj(mlTradeAccount.getKqZj().subtract(mlAccountAssetApply.getCash()));
        this.updateById(mlAccountAssetApply);
        iMlTradeAccountService.updateById(mlTradeAccount);
        iMlAssetFlowService.creatObject(mlTradeAccount.getTdAcc(),5,mlAccountAssetApply.getCash().multiply(new BigDecimal(-1)));
        return 1;
    }


}
